<template>
  <div class="adminFeedback">
    <div class="adminMsg">
      <h2>反馈管理</h2>
      <div class="title block">
        <span>反馈列表</span>
        <span>操作</span>
      </div>
      <div v-for="item in currentList" :key="item.id" >
        <div class="info block">
          <div class="msg">
            <p>反馈人：{{ item.sendName }}</p>
            <p v-if="role==='管理员'">反馈人邮箱：{{ item.email }}</p>
            <p v-if="role==='管理员'">意见内容：{{ item.content }}</p>
            <p v-if="role==='房东'">反馈内容：<p style="font-size: medium;font-weight: 600">{{ item.content }}</p>
            <p v-if="role==='房东'">反馈房屋：{{item.houseTitle}}</p>
            <p v-if="role==='管理员'">反馈人：{{ item.sendTime }}</p>
            <p v-if="role==='房东'">反馈时间：{{ item.createTime }}</p>
            <p v-if="role==='房东'">回复人：{{ item.replyName }}</p>
            <p v-if="role==='房东'">回复内容：<p style="font-size: medium;font-weight: 600">{{item.replyContent}}</p>
            <p v-if="role==='房东'">
              回复状态：
              <el-tag v-if="item.status === 0" effect="dark" type="success">待回复</el-tag>
              <el-tag v-if="item.status === 1" effect="dark" type="danger">已回复</el-tag>
            </p>
            <p v-if="role==='房东'">回复时间：{{ item.replyTime }}</p>
          </div>
          <div v-if="item.status===1 && role==='房东'" class="btn" >
            <el-button style="font-size: 13px;font-weight: 500;background-color: deepskyblue;color: white">再次回复</el-button>
          </div>
          <div v-else class="btn">
            <el-button style="font-size: 13px;font-weight: 500;" type="success" @click="reply(item)">回复</el-button>
          </div>
        </div>
        <el-dialog
            :visible.sync="dialogVisible"
            title="反馈回复"
            width="50%"
            @closed="handleDialogClose">
          <el-form :model="currentFeedback" label-width="100px">
            <!-- 原反馈内容（只读） -->
            <el-form-item class="feedback-content"  label="反馈内容：">
              <el-input
                  v-model="item.content"
                  :disabled="true"
                  :rows="3"
                  class="highlight-feedback"
                  type="textarea">
              </el-input>
            </el-form-item>
            <!-- 回复内容输入 -->
            <el-form-item class="feedback-content" label="回复内容："  prop="replyContent">
              <el-input
                  v-model="currentFeedback.replyContent"
                  :rows="5"
                  maxlength="500"
                  placeholder="请输入回复内容"
                  show-word-limit
                  type="textarea">
              </el-input>
            </el-form-item>
          </el-form>

          <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="submitReply">确 定</el-button>
    </span>
        </el-dialog>
      </div>
      <div class="Page">
        <el-pagination :current-page.sync="pageNum"
                       :page-size="pageSize"
                       :page-sizes="[3, 5, 7, 9, 20]"
                       :total="allList.length"
                       layout="total, sizes, prev, pager, next"
                       @current-change="handlePage"
                       @size-change="handleSizeChange"
                       >
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import axios from "axios";

const user = JSON.parse(localStorage.getItem("login-user"))

export default {
  name: "AdminMyFeedback",

  created() {
    this.getRole();
  },
  mounted() {
    if(this.role==='房东'){
      this.getList();
    }else if (this.role==='管理员'){
      this.getList2();
    }
  },
  data() {
    return {
      dialogVisible: false,
      currentFeedback: { // 当前操作的反馈
        content: '',
        replyContent: ''
      },
      role:'',
      list: [],
      allList: [],
      pageNum: 1,
      pageSize: 3,
      currentList:[],
    }
  },
  methods: {

    handleSizeChange(size) {

      this.pageSize = size;
      this.pageNum = 1; // 重置到第一页
      this.handlePage(1,size)
    },

    handlePage(page) {
      this.pageNum = page;
      const start = (page - 1) * this.pageSize;
      const end = page * this.pageSize;
      this.currentList = this.allList.slice(start, end);
    },

    getRole(){
      axios({
        url:"http://localhost:8090/user/getUserRole",
        methods: 'get'
      }).then(res=>{
        if (res.code === '200'){
          this.role = res.data
          if(res.data==='房东'){
            this.getList();
          }else if (res.data==='管理员'){
            this.getList2();
          }
        }else{
          this.$message.error(res.msg)
        }
      })
    },

    getList() {
      axios({
        url: "http://localhost:8090/feedback/list",
        method: 'get',
      }).then(res => {
        if(res.code === '200'){
          this.allList = res.data;
          this.handlePage(1);
        }
      })
    },

    getList2() {
      axios({
        url: "/feedback/getAdvice",
        method: 'get',
      }).then(res => {
        if(res.code === '200'){
          this.allList = res.data;
          this.handlePage(1);
        }
      })
    },

    // 点击回复按钮
    reply(item) {
      this.currentFeedback = {
        id: item.id,
        content: item.content,
        replyContent: item.replyContent || '' // 保留已有回复内容
      }
      this.dialogVisible = true
    },
    // 提交回复
    async submitReply() {
      if (!this.currentFeedback.replyContent.trim()) {
        this.$message.warning('请填写回复内容')
        return
      }
      await axios({
        url: 'feedback/reply',
        method:'post',
        data:{
          id:this.currentFeedback.id,
          replyContent:this.currentFeedback.replyContent,
        }
      }).then(res=>{
        if (res.code==='200'){
          this.$message.success('回复成功')
          this.handleDialogClose()
          this.getList()
        }else{
          this.$message.error(res.msg)
        }
      })
    },

    // 关闭对话框时重置数据
    handleDialogClose() {
      this.currentFeedback = {
        replyContent: ''
      }
      this.dialogVisible = false
    }
  },
}
</script>

<style scoped>
.adminFeedback{
  width: 1110px;
  min-height: 750px;
  background-color: white;
  padding: 20px 50px;
}

.adminMsg{
  width: 1155px;
  min-height: 850px;
  background-color: white;
  border-radius: 15px;
  padding: 20px 90px 10px 20px;
  box-shadow: 0 0px 14px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .04)
}

.adminMsg h2 {
  font-size: 20px;
  font-weight: 600;
  margin-buttom: 15px;
  margin-left: 0px;
}
.Page {
  display: flex;
  justify-content: center;
  margin-bottom: 20px;
}

.block{
  display: block;
  display: flex;
  align-items: center;
  width: 1195px;
}
.block> :first-child{
  flex: 4;
}

.block> :nth-child(n+2){
  flex: 1;
  text-align: center;
}

.title {
  height: 50px;
  background-color: #4b5fff;
  color: white;
  padding-left: 20px;
  margin-top: 20px;
}

.info {
  height: 380px;
  padding: 30px 20px 30px 0;
  border-bottom: 1px solid #eee;
}

.info .msg {
  color: #777;
  line-height: 25px;
  margin-left: 18px;
}

.info .btn {
  margin-left: 100px;
}

.msg h3 {
  font-size: 18px;
  color: black;
  font-weight: 600;
  margin-bottom: 5px;
}



.highlight-feedback >>> .el-textarea__inner {
  /* 字体样式 */
  font-size: 16px;
  font-weight: 600;
  color: #e6a23c;  /* Element的warning颜色 */

  /* 背景和边框 */
  background-color: #fdf6ec;  /* 浅橙色背景 */
  border: 2px solid #e6a23c;
  border-radius: 4px;

  /* 内边距 */
  padding: 12px;

  /* 禁用状态覆盖 */
  cursor: not-allowed;
  opacity: 1;  /* 保持完全可见 */
}

/* 调整标签样式 */
.feedback-content >>> .el-form-item__label {
  font-size: 16px;
  color: #303133;
  font-weight: bold;
  padding-bottom: 8px;
}
</style>